/*
 * @Author: your name
 * @Date: 2021-02-28 13:41:10
 * @LastEditTime: 2021-04-28 08:39:52
 * @LastEditors: Please set LastEditors
 * @Description: 新增子目录
 * @FilePath: \jianfa-api\dao\menuDao.js
 */
const { querySql } = require("../db");
const moment = require("moment");
moment.locale("zh-cn");
const { page_sql } = require("../utils/utils");
module.exports = {
    /**
     * @description: 新增三级子目录
     * @param {cid} 父亲id
     * @param {type} img or pdf
     * @param {class_name} 目录名字
     * @param {table_name} 表名称
     * @return {*}
     */
    async addMenuDao({ cid, type, class_name, table_name }) {
        const sql = `insert into ${table_name} (cid, type , class_name , sort) values ('${cid}' , '${type}', '${class_name}' , ${Date.now()})`;
        return await querySql(sql);
    },
    //添加项目工程图片
    /**
     * @description:
     * @param {*} filesInfo //文件信息集合
     * @param {*} sid /父表的id
     * @param {string} table_name /表名
     * @return {Promise}
     */
    async addMenuFileDao(filesInfo, sid, table_name) {
        let insertStr = "";
        filesInfo.forEach((item) => {
            insertStr += `('${item.name}','${item.filePath}','${sid}','${moment().format("YYYY年MM月D日 h:mm:ss")}'),`;
        });
        let sql = `insert into ${table_name} (file_name, file_path , sid , create_time) values ` + insertStr;
        sql = sql.substr(0, sql.length - 1);
        return await querySql(sql);
    },
    //获取三级菜单
    async getMenuDao({ table_name, cid }) {
        const sql = `select * from ${table_name} where cid=${cid}`;
        return await querySql(sql);
    },
    //修改三级菜单
    async updateMenuDao({ table_name, id, sort }) {
        const sql = `UPDATE ${table_name} SET sort = '${sort}' WHERE id = '${id}' `;

        return await querySql(sql);
    },
    //获取二级菜单
    async getMenuTowDao({ table_name }) {
        const sql = `select * from ${table_name}`;
        return await querySql(sql);
    },
    //获取三级菜单的文件数据
    async getMenuFileDao(params) {
        const { table_name, sid, id } = params;
        let sql = `select * from ${table_name}`;
        if (id) {
            sql += ` where id=${id}`;
            return await querySql(sql);
        }
        if (!sid) {
            return {
                results: [],
                status: true,
            };
        }
        return await querySql(await page_sql(params, sql, table_name));
    },
    //获取三级菜单的文件数据不分页
    async getMenuFileNoPageDao(params) {
        const { table_name, sid } = params;
        let sql = `select * from ${table_name} where sid='${sid}'`;
        return await querySql(sql);
    },
    //根据id删除三级菜单的文件数据
    async delMenuFileDao(params) {
        const { table_name, id } = params;
        const sql = `delete from ${table_name} where id='${id}'`;
        return await querySql(sql);
    },
    //根据id删除三级菜单
    async delMenuDao(params) {
        const { table_name, id } = params;
        const sql = `delete from ${table_name} where id='${id}'`;
        return await querySql(sql);
    },
};
